package com.moli.iov.producer;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Properties;

/**
 * @ClassName KafkaProducerUtil
 * @Description TODO
 * @Author lixy
 * @Date 2019/5/15 16:54
 **/
@Component
public class KafkaProducerUtil {

    private String springBaseXmlPath;
    /**
     * 初始化kafka生产者信息
     * @return
     */
    public Properties getProps(){
        ApplicationContext context = new ClassPathXmlApplicationContext(springBaseXmlPath);
        Map<String,Object> map=(Map<String,Object>)context.getBean("producerProperties");
        Properties props = new Properties();
        props.put("bootstrap.servers", map.get("bootstrap.servers").toString());
        props.put("acks",  map.get("acks").toString());
        props.put("retries",Integer.parseInt(map.get("retries").toString()));
        props.put("batch.size", Integer.parseInt(map.get("batch.size").toString()));
        props.put("linger.ms",Integer.parseInt(map.get("linger.ms").toString()));
        props.put("buffer.memory", Integer.parseInt(map.get("buffer.memory").toString()));
        props.put("key.serializer", map.get("key.serializer").toString());
        props.put("value.serializer", map.get("value.serializer").toString());
        return props;
    }

    public void setStringBaseXMLPath(String springBaseXmlPath){
        this.springBaseXmlPath=springBaseXmlPath;
    }
}
